<?php 
class CommentModel extends Model
{
		static $messageType = array(
			'1' => '_MESSAGE_CONSULTING_SERVICE_',
			'2' => '_MESSAGE_SUGGEST_',
			'3' => '_MESSAGE_AFTER_SALES_',
			'4' => '_MESSAGE_COMPLAINT_',
		);
		
		static $zixunType = array(
			'5' => 'Commodity Advisory Services',
			'6' => 'Inventory and distribution',
			'7' => 'pay off questions',
			'8' => 'Presents And Promotion Gifts',
		);
	/**
	 * 获取产品评论列表
	 * @param int $pid 产品ID
	 */
	public static function get_product_comment_list($pid,$did=0)
	{
		$com_db = D('comment');
		$com_db->field('id,username,email,title,content,star,comment_time,comment_type');
		if($did>0)
			$com_db->where('disabled=1 and parent_id=0 and dproduct_id='.$did);
		else
			$com_db->where('disabled=1 and parent_id=0 and product_id='.$pid);
		$commentList = $com_db->order('id DESC')->select();
		//echo $com_db->getLastSql();
		if($commentList){
			foreach ($commentList as &$com){
				$com_db->field('id,user_id,username,email,content,star,comment_time,comment_type');
				$com_db->where('parent_id='.$com['id']);
				$com['subList'] = $com_db->order('id ASC')->select();
			}
			unset($com);
		}	

		return $commentList;
	}
	
	/**
	 * 获取产品评论数量
	 * @param int $pid 产品ID
	 */
	public static function get_product_comment_count($pid,$did=0)
	{
		$com_db = D('comment');
		if($did>0)
			$com_db->where('disabled=1 and parent_id=0 and dproduct_id='.$did);
		else
			$com_db->where('disabled=1 and parent_id=0 and product_id='.$pid);
		$count = $com_db->count();
		return $count;
	}

	/**
	 * 获取产品评论星星 平均数
	 * @param int $pid 产品ID
	 */
	public static function get_product_comment_star($pid,$did=0)
	{
		$com_db = D('comment');
		if($did>0)
			$where = 'disabled=1 and parent_id=0 and dproduct_id='.$did;
		else
			$where = 'disabled=1 and parent_id=0 and product_id='.$pid;
		$count = $com_db ->where($where) ->count();
		$star = $com_db ->where($where)->getField('sum(star)');
		return ceil($star/$count);
	}
	
	/**
	 * 添加评论
	 * @param array $post POST列表
	 */
	public static function insert_comment($post)
	{
		global $user;
		$data = array(
			'user_id' => isset($user['id']) ? $user['id'] : 0,
			'designer_id' => isset($post['designer_id']) ? intval($post['designer_id']) : 0,
			'product_id' => isset($post['product_id']) ? intval($post['product_id']) : 0,
			'dproduct_id' => isset($post['dproduct_id']) ? intval($post['dproduct_id']) : 0,
			'username' => isset($post['username']) ? trim($post['username']) : '',
			'email' => isset($post['email']) ? trim($post['email']) : '',
			'title' => isset($post['title']) ? trim($post['title']) : '',
			'content' => trim($post['content']),
			'star' => isset($post['star']) ? intval($post['star']) : 0,
			'order_id' => isset($post['order_id']) ? intval($post['order_id']) : 0,
			'ip' => get_client_ip(),
			'comment_time' => time(),
			'disabled' => intval(C('comment_disabled')) ? 0 : 1,
			'comment_type' => isset($post['comment_type']) ? $post['comment_type'] : 'up',
			'order_items_id' => isset($post['order_items_id']) ? $post['order_items_id'] : 0,
		);
		$com_db = D('comment');
		$com_id = $com_db->add($data);
		
		//更新商品顶踩
		if($com_id && $data['product_id']){
			ProductModel::update_product_up_down($data['product_id'], $data['comment_type']);
		}
		
		//更新作品顶踩
		if($com_id && $data['dproduct_id']){
			$designer_product_db = D('designer_product');
			$productInfo = $designer_product_db->find($data['dproduct_id']);
			if(!$productInfo)return false;
			$field = $data['comment_type']=='up' ? 'designer_up' : 'designer_down';
			$designer_product_db->where('id='.$data['dproduct_id'])->setInc($field);
		}
		return $com_id;
	}
	
	/**
	 * 添加留言
	 * @param array $post POST列表
	 */
	public static function insert_guestbook($post)
	{
		global $user,$language_id;
		$data = array(
			'message_type' => intval($post['message_type']),
			'product_id' => isset($post['product_id']) ? $post['product_id'] : 0,
			'title' => trim($post['title']),
			'content' => trim($post['content']),
			'user_id' => isset($post['user_id']) ? $post['user_id'] : (isset($user['id']) ? $user['id'] : 0),
			'language_id' => isset($post['language_id']) ? $post['language_id'] : $language_id,
			'username' => isset($post['username']) ? trim($post['username']) : (isset($user['username']) ? $user['username'] : ''),
			'email' =>  isset($post['email']) ? trim($post['email']) : (isset($user['email']) ? $user['email'] : ''),
			'data' =>  (isset($post['data']) && is_array($post['data'])) ?  serialize($post['data']) : '',
			'ip' => get_client_ip(),
			'start_time' => time(),
			'disabled' => intval(C('comment_disabled')) ? 0 : 1,
		);
		$guestbook_db = D('guestbook');
		$gbid = $guestbook_db->add($data);
		return $gbid;
	}
	
	/**
	 * 获取留言列表
	 * @param array $post
	 */
	public static function get_guestbook_list($post, $page=1, $pageRows = 0)
	{
		$filter = array(
			'message_type' => isset($post['message_type']) ? $post['message_type'] : null,
			'product_id' => isset($post['product_id']) ? $post['product_id'] : null,
			'user_id' => isset($post['user_id']) ? $post['user_id'] : null,
			'language_id' => isset($post['language_id']) ? $post['language_id'] : null,
			'disabled' => isset($post['disabled']) ? $post['disabled'] : 1,
		);
		
		foreach ($filter as $fk => $fv){
			if(is_null($fv)){
				unset($filter[$fk]);
			}
		}
		
		$guestbook_db = D('guestbook');
		$guestbook_db->where($filter);
		if($pageRows){
			$guestbook_db->page($page, $pageRows);
		}
		$guestbookList = $guestbook_db->select();
//		echo $guestbook_db->getLastSql();exit;
		return $guestbookList ? $guestbookList : array();
	}
	
	/**
	 * 获取作品详情
	 * @param int $pid 产品ID
	 */
	public static function get_designer_product_info($pid)
	{
		$designer_product = D('designer_product');
		$designer_product->where('product_id='.$pid);
		$designerInfo = $designer_product->find();
		return $designerInfo;
	}
}